/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-07 19:44:56
 * Copyright (c) 2015- joysuch-版权所有
 */

package com.joysuch.wwyt.bp.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.joysuch.wwyt.bp.bean.BpWorkSafetyApplyMethodEditBean;
import com.joysuch.wwyt.bp.bean.safetyworkcert.BpWorkSafetyCertDto;
import com.joysuch.wwyt.bp.entity.BpWorkSafetyCertMethod;
import com.joysuch.wwyt.bp.repository.BpWorkSafetyCertMethodDao;
import com.joysuch.wwyt.bp.service.BpContractorStaffService;
import com.joysuch.wwyt.bp.service.BpWorkSafetyCertMethodService;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.common.util.PageDataUtils.PageDataConvert;
import com.joysuch.wwyt.core.exception.BusinessRestrictionsException;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import com.joysuch.wwyt.edu.repository.EduCertificateDao;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class BpWorkSafetyCertMethodServiceImpl implements BpWorkSafetyCertMethodService {

    @Autowired
    private BpWorkSafetyCertMethodDao dao;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;
    @Autowired
    private BaseUserDao baseUserDao;
    @Autowired
    private EduCertificateDao eduCertificateDao;
    @Autowired
    private BpContractorStaffService bpContractorStaffService;

    @Override
    public void delete(Long id) throws BusinessRestrictionsException {
        // 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用。不能删除时将抛出业务异常
        checkBussinessRestrictions(id);
        dao.deleteById(id);
    }

    /**
     * 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用
     */
    private void checkBussinessRestrictions(Long id) throws BusinessRestrictionsException {

    }

    @Override
    public BpWorkSafetyCertMethod findById(Long id) {
        return dao.findById(id).get();
    }

    @Override
    public List<BpWorkSafetyApplyMethodEditBean> findByCertTypeAndId(Long workId) {
        List<BpWorkSafetyCertMethod> methods = dao.findByWorkId(workId);

        return PageDataUtils.convertListData(methods,
                new PageDataConvert<BpWorkSafetyApplyMethodEditBean, BpWorkSafetyCertMethod>() {

                    @Override
                    public BpWorkSafetyApplyMethodEditBean convert(BpWorkSafetyCertMethod f) {
                        BpWorkSafetyApplyMethodEditBean bean = new BpWorkSafetyApplyMethodEditBean();

                        List<Long> longs = JSON.parseArray(f.getConfirmUserCertIdJson(), Long.class);
                        List<BpWorkSafetyCertDto> confirmUserCertNames = Lists.newArrayList();
                        if (longs != null) {
                            for (Long aLong : longs) {
                                confirmUserCertNames.add(new BpWorkSafetyCertDto(aLong, eduCertificateDao.getNameById(aLong)));
                            }
                        }
                        bean.setConfirmUserCertNames(confirmUserCertNames);
                        if (f.getConfirmUserId() != null) {
                            if (f.getConfirmUserType() == null) {
                                bean.setConfirmUserName(getName(baseUserDao.getBaseUserNameById(f.getConfirmUserId()),getCertNameList(bean.getConfirmUserCertNames())));
                            } else if (f.getConfirmUserType() == 1) {
                                bean.setConfirmUserName(getName(bpContractorStaffService.getNameById(f.getConfirmUserId()),getCertNameList(bean.getConfirmUserCertNames())));
                            } else {
                                bean.setConfirmUserName(getName(baseUserDao.getBaseUserNameById(f.getConfirmUserId()),getCertNameList(bean.getConfirmUserCertNames())));
                            }
                            bean.setConfirmUserId(f.getConfirmUserId());
                        }

                        bean.setMethod(f.getMethod());
                        bean.setId(f.getId());
                        bean.setState(f.getState());
                        if (f.getState() == null) {
                            bean.setStateName("");
                        } else if (f.getState() == 0) {
                            bean.setStateName("已确认");
                        } else if (f.getState() == 1) {
                            bean.setStateName("不涉及");
                        } else if (f.getState() == 2) {
                            bean.setStateName("不符合");
                        } else {
                            bean.setStateName("");
                        }
                        bean.setUserType(f.getConfirmUserType());
                        return bean;
                    }

                });
    }

    private List<String> getCertNameList(List<BpWorkSafetyCertDto> certNames) {
        List<String> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(certNames)) {
            for (BpWorkSafetyCertDto certDto : certNames) {
                String name = eduCertificateDao.getNameById(certDto.getCertId());
                String code = eduCertificateDao.getCodeById(certDto.getCertId());
                list.add(name + "(" + code + ")");
            }

        }
        return list;
    }

    private String getName(String name, List<String> list) {
        if (!CollectionUtils.isEmpty(list)) {
            return name + "(" + Joiner.on(",").join(list) + ")";
        } else {
            return name;
        }
    }

}
